package com.cai310.lottery.service.lottery.keno.klsf.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Calendar;
import java.util.Date;

import javax.annotation.Resource;

import com.cai310.lottery.common.Lottery;
import com.cai310.lottery.common.SalesMode;
import com.cai310.lottery.entity.lottery.keno.klsf.KlsfIssueData;
import com.cai310.lottery.entity.lottery.keno.klsf.KlsfScheme;
import com.cai310.lottery.exception.DataException;
import com.cai310.lottery.prizeutils.keno.KlsfPrizeWork;
import com.cai310.lottery.service.lottery.keno.KenoService;
import com.cai310.lottery.service.lottery.keno.impl.KenoPlayerImpl;
import com.cai310.lottery.support.ContentBean;
import com.cai310.lottery.support.klsf.KlsfContentBeanBuilder;
import com.cai310.lottery.support.klsf.KlsfPlayType;
import com.cai310.utils.DateUtil;
import org.springframework.stereotype.Component;

@Component("klsfKenoPlayer")
public class KlsfKenoPlayer extends KenoPlayerImpl<KlsfIssueData, KlsfScheme> {
	@Override
	public Lottery getLottery() {
		return Lottery.KLSF;
	}

	@Resource(name = "klsfKenoServiceImpl")
	@Override
	public void setKenoService(KenoService<KlsfIssueData, KlsfScheme> kenoService) {
		this.kenoService = kenoService;
	}

	public void calculatePrice(KlsfScheme scheme, String results) throws DataException {
		KlsfPrizeWork klsfPrizeWork = null;
		if(SalesMode.COMPOUND.equals(scheme.getMode())){
			klsfPrizeWork = new KlsfPrizeWork(scheme.getCompoundContent(), scheme.getMultiple(), results,scheme.getBetType());
		}else if(SalesMode.SINGLE.equals(scheme.getMode())){
			klsfPrizeWork = new KlsfPrizeWork(scheme.getContent(), scheme.getMultiple(), results,scheme.getBetType());
		}
		if (klsfPrizeWork.getKlsfWin().isWon()) {
			// 更新
			scheme.setWon(Boolean.TRUE);
			scheme.setPrize(new BigDecimal(klsfPrizeWork.getKlsfWin().getPrize()));
			scheme.setPrizeAfterTax(new BigDecimal(klsfPrizeWork.getKlsfWin().getPrizeAfterTax()));
			scheme.setPrizeDetail(klsfPrizeWork.getKlsfWin().getPrizeDetail());
		}
	}

	public void calculateSchemeUnits(KlsfScheme scheme) throws DataException {
		KlsfPlayType klsfPlayType = scheme.getBetType();
		ContentBean contentBean = KlsfContentBeanBuilder.buildCompoundContentBean(scheme.getContent(), klsfPlayType);
		scheme.setUnits(contentBean.getUnits());

	}

	public KlsfIssueData createNowIssueData(String issueNumber) {
		return new KlsfIssueData();
	}

	public Date getBeginTimeByIssueNumber(String issueNumber) {
		String strDate = issueNumber.substring(0, 8);
		String strPeriod = issueNumber.substring(8, 10);
		int period = Integer.valueOf(strPeriod);
		Calendar cal = Calendar.getInstance();
		cal.setTime(DateUtil.strToDate(strDate, "yyyyMMdd"));
		int startHour = 9;
		int offsetMinute = (period - 1) * 10;
		int currHour = startHour + (offsetMinute / 60);
		int leftMinute = offsetMinute % 60;
		cal.set(Calendar.HOUR_OF_DAY, currHour);
		cal.set(Calendar.MINUTE, leftMinute);
		return cal.getTime();
	}

	public String issueNumberAssembly(Date day, int period) {
		NumberFormat numFmt = new DecimalFormat("00");
		String nstr = numFmt.format(period);
		String dTimeStr = DateUtil.getFormatDate("yyyyMMdd", day);
		String issueNumber = dTimeStr + nstr;
		return issueNumber;
	}

	@Override
	public int getMaxPeriod() {
		return 84;
	}


	@Override
	public int getPeriodMinutes() {
		return 10;
	}

	@Override
	public boolean calculatePrice(SalesMode salesMode, String content,
			Integer multiple, String betType, String results)
			throws DataException {
		return false;
	}

	@Override
	public int getBeforeTime() {
		return 1;
	}

	@Override
	public int getBeforeSecondsTime() {
		return 30;
	}
}
